package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.ZrHandphonePhotoDao;
import com.hhs.storage.service.ImgService;
import com.hhs.storage.service.ZrHandphonePhotoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.SysUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chx
 * @since 2024-02-23
 */
@Service
public class ZrHandphonePhotoServiceImpl extends ServiceImpl<ZrHandphonePhotoDao, ZrHandphonePhoto> implements ZrHandphonePhotoService {
    @Resource
    ImgService imgService;
    @Override
    public ResponseBean<ZrHandphonePhoto> savePhoto(ZrHandphonePhoto zrHandphonePhoto, MultipartFile[] photoList, String deleIdStr, HttpServletRequest request) {
        zrHandphonePhoto.setCreateTime(LocalDateTime.now());
        save(zrHandphonePhoto);
        imgService.upload(SysUtil.HANDPHONE,zrHandphonePhoto.getId(),photoList);
        return ResponseBean.success(null);
    }

    @Override
    public ResponseBeanPage<ZrHandphonePhoto> table(InputParam inputParam) {
        QueryWrapper<ZrHandphonePhoto> qw =new QueryWrapper<>();
        qw.orderByAsc("sort");
        qw.orderByDesc("create_time");
        Page<ZrHandphonePhoto> page = page(new Page<ZrHandphonePhoto>(inputParam.getPage(), inputParam.getSize()), qw);
        List<ZrHandphonePhoto> rows = page.getRecords();
        for (ZrHandphonePhoto zh :
                rows) {
            List<Img> list = imgService.getByMainId(new Img(zh.getId()));
            zh.setPhList(list);
        }
        ResponseBeanPage res=ResponseBeanPage.getInstance();
        res.setRows(rows);
        res.setCode(1);
        res.setTotal(page.getTotal());
        return res;
    }
}
